<?php

declare (strict_types = 1);

namespace app\common\service\user;

use app\common\repository\user\MechanismRepository;
use app\common\service\BaseService;
use app\common\exception\AdminException;
use app\common\repository\user\UserMiningProfitLogRepository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;

class UserMiningProfitLogService extends BaseService
{
    public function __construct(UserMiningProfitLogRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取列表
     *
     * @param array $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getList(array $data = []): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        $where = [];
        if(!empty($data['mode'])) {
            $where['mode'] = $data['mode'];
        }
        return $this->repository->paginate($where, ['create_time', 'desc'], $page, $pageSize);
    }

    /**
     * 获取详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id)
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建信息
     *
     * @param array $params
     * @return \app\common\model\BaseModel|array|\think\Collection|\think\model\contract\Modelable
     * @throws \Exception
     */
    public function create(array $params)
    {
        $data = [
            'tag_name' => $params['tag_name'],
            'cate_id'  => $params['cate_id'],
            'status' => $params['status'],
        ];
        return $this->repository->save($data);
    }

    /**
     * 更新信息
     *
     * @param array $params
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $params, $id)
    {
        if(empty($id)) {
            throw new AdminException('ID 不能为空');
        }
        $data = [
            'tag_name' => $params['tag_name'],
            'cate_id'  => $params['cate_id'],
            'status' => $params['status'],
        ];
        return $this->repository->update($data, $id);
    }

    /**
     * 删除信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): bool
    {
        return $this->repository->delete($id);
    }

    /**
     * 获取用户收益记录
     *
     * @param $user_id
     * @param $mode
     * @param $invId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function getUserMiningProfitLog($user_id, $mode, $invId): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        $where = ['user_id' => $user_id];
        if(!empty($mode) && $invId) {
            $where['mode'] = $mode;
        }
        if(!empty($invId) && $invId) {
            $where['investment_id'] = $invId;
        }
        $data = $this->repository->paginate($where, ['create_time' => 'desc'], $page, $pageSize);
        $mechanismRepository = app()->make(MechanismRepository::class);
        foreach($data['list'] as &$item) {
            $item['desc'] = $mechanismRepository->getValue('desc', $item['plan_id']);
        }

        return $data;
    }
}